home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / tblt / tblt⁄event.c < prev    next >
C/C++ Source or Header  |  1986-09-08  |  7KB  |  213 lines

  1. /*
  2.  * event.c - startup, stopping, and the main event loop
  3.  */
  4.  
  5. #include <quickdraw.h>
  6. #include <memory.h>
  7. #include <font.h>
  8. #include <window.h>
  9. #include <dialog.h>
  10. #include <osutil.h>
  11. #include <menu.h>
  12. #include <event.h>
  13. #include <textedit.h>
  14. #include <desk.h>
  15. #include <control.h>
  16. #include <toolutil.h>
  17.  
  18. #define DATA
  19. #include "tablut.h"
  20.  
  21. /*
  22.  * main() - start the program.
  23.  */
  24. main()
  25. {
  26.     static WindowRecord wrecord;    /* Storage for window record. */
  27.     
  28.     /*
  29.      * Initialize our program.  It seems best to handle:
  30.      * Memory inits first, ToolBox inits second, then the program variables'
  31.      * inits. Note that the order of inits is important; see "Using the
  32.      * Dialog Manager" in the Dialog Mgr section.
  33.      */
  34.  
  35.     setupmemory();
  36.     InitGraf(&thePort); InitFonts(); InitWindows(); InitMenus(); TEInit();
  37.     InitDialogs((ProcPtr) 0); InitCursor();
  38.     SetEventMask(everyEvent - keyUpMask); /* set default event mask */
  39.  
  40.     /* 
  41.      * Get the port which is the whole screen, to use when deactivating
  42.      * our window.  This prevents the current grafPort pointer from
  43.      * ever dangling.
  44.      */
  45.     GetWMgrPort(&screenport);SetPort(screenport);
  46.  
  47.     if (seeanyfiles()) {    /* read the finder file info    */
  48.     if (printanyfiles()) {    /* print requested files & exit */
  49.         return;
  50.     }
  51.     }
  52.  
  53.     /* 
  54.      * GetNewWindow posts an update event for the new window,
  55.      * so it will be redrawn right away. (-1 is the frontmost window)
  56.      * Set the default text drawing for our window.
  57.      */
  58.     mywindow = GetNewWindow(WINDOWID, &wrecord, (long) -1);
  59.     SetPort(mywindow); TextFont(systemFont);
  60.  
  61.     setupmenus();    /* pull in and setup our menus        */
  62.     getboard();        /* init the board picture        */
  63.     getstate();        /* init the game status strings        */
  64.     makegrid();        /* create the representation of the board */
  65.     getpieces();    /* init the playing pieces' pictures    */
  66.     makehomes();    /* create the pieces' home locations    */
  67.     getcontrols();    /* init the game controls        */
  68.     getcursors();    /* init the game's cursors        */
  69.     initmoves();    /* init the recording of games        */
  70.     drawwindow();    /* draw everything that has been set up    */
  71.     if (!openanyfiles()) { /* read the requested game or...    */
  72.     newgame();    /* ...start a new one            */
  73.     }
  74.     while (doevent())    /* do the event-driven part of the program */
  75.     ;
  76.     closeup();
  77. }
  78.  
  79. /*
  80.  * closeup() - prepare to exit the program. (called before exiting).
  81.  * Restore the state of the machine so the finder will be happy.
  82.  */
  83. closeup()
  84. {
  85.     InitCursor();
  86.     SetEventMask(everyEvent - keyUpMask);
  87. }
  88.  
  89. /*
  90.  * doevent() - one loop through the main event handler
  91.  *   Returns 1 if the user wants to continue; 0 if it's time to quit
  92.  */
  93. int
  94. doevent()
  95. {
  96.     GrafPtr saveport;
  97.     EventRecord myevent;
  98.     WindowPtr whichwindow;    /* Points to window of MouseDown    */
  99.     DialogPtr whichdialog;    /* dialog of MouseDown            */
  100.     short whichitem;        /* mouse's dialog item            */
  101.     int windowcode;        /* What mouse was in when event posted. */
  102.     ControlHandle whichcontrol; /* the control that was pressed        */
  103.     short partcode;        /* the pressed part of that control    */
  104.     Point pt;            /* temp point                */
  105.     KeyMap keys;        /* temp key bitlist            */
  106.     int pieceid;        /* index of the selected piece (-1 if none) */
  107.     Point newgrid;        /* board grid coords of piece's destination */
  108.     int userdone;        /* True when user wants to exit program    */
  109.  
  110.     SystemTask();        /* give the system a moment        */
  111.     userdone = 0;
  112.  
  113.     /*
  114.      * Track the realtime objects: the cursor type.
  115.      * If we had a clipboard, we'd update its window here.
  116.      * If we had crosshairs (like MacDraw), we'd update them here.
  117.      */
  118.  
  119.     GetMouse(&pt); GetKeys(keys); fixcursor(&pt, keys);
  120.  
  121.     if (!GetNextEvent(everyEvent, &myevent)) {
  122.     (void) IsDialogEvent(&myevent);        /* blinks the edit point */
  123.     } else if ((myevent.modifiers & cmdKey) &&
  124.       (myevent.what == keyDown || myevent.what == autoKey)) {
  125.     /* handle command keys here -- dialogs can't. */
  126.     userdone = docommand(MenuKey(
  127.       (char) (myevent.message & charCodeMask)));
  128.     } else if (IsDialogEvent(&myevent)) {
  129.     if (DialogSelect(&myevent, &whichdialog, &whichitem)) {
  130.         /* if we had modeless dialogs, we'd handle their events here */
  131.     }
  132.     } else {
  133.     switch (myevent.what) {
  134.     case mouseDown:
  135.         windowcode = FindWindow(pass(myevent.where), &whichwindow);
  136.         switch (windowcode) {
  137.         case inSysWindow:            /* a DA's window */
  138.         SystemClick(&myevent, whichwindow);
  139.         break;
  140.         case inMenuBar:            /* some other menu */
  141.         userdone = docommand(MenuSelect(pass(myevent.where)));
  142.         break;
  143.         case inGoAway:            /* GoAway region */
  144.         if (TrackGoAway(whichwindow, pass(myevent.where))) {
  145.             /* if we could close our window, we'd do it here */
  146.         }
  147.         break;
  148.         case inDrag:            /* Title Bar */
  149.         if (whichwindow != FrontWindow() &&
  150.           (myevent.modifiers & cmdKey) == 0) {
  151.             SelectWindow(whichwindow);
  152.         }
  153.         /* If we could drag our window, we'd do it here */
  154.         break;
  155.         case inGrow:            /* Grow Region */
  156.         /*
  157.          * because we don't have a grow region, we treat it
  158.          * as part of the window's content.
  159.          */
  160.          
  161.         case inContent:            /* Somewhere in the window */
  162.         if (whichwindow != FrontWindow()) {
  163.             /* the window isn't active - activate it */
  164.             SelectWindow(whichwindow);
  165.         } else {
  166.             /* the window is active, use it */
  167.             GlobalToLocal(&myevent.where);
  168.             if (whichwindow == mywindow) {
  169.             if ((partcode = FindControl(pass(myevent.where),
  170.              whichwindow, &whichcontrol))) {
  171.                 docontrol(whichcontrol, partcode,
  172.                   &myevent.where);
  173.             } else if ((pieceid =
  174.               findpiece(&myevent.where)) >= 0) {
  175.                 dragpiece(pieceid, &myevent.where);
  176.                 newgrid.h = 
  177.                   piece[pieceid].prevlook.bounds.left
  178.                   + piece[pieceid].class->etoc.h;
  179.                 newgrid.v = 
  180.                   piece[pieceid].prevlook.bounds.top
  181.                   + piece[pieceid].class->etoc.v;
  182.                 pointtogrid(&newgrid);
  183.                 movepiece(pieceid, &newgrid);
  184.             }
  185.             }
  186.         }
  187.         break;
  188.         }
  189.         break;
  190.     case keyDown: 
  191.     case autoKey:
  192.         /* if the user could type, here's where we'd handle that.    */
  193.         break;
  194.     case updateEvt:        /* update the appropriate window */
  195.         if ((WindowPtr)(myevent.message) == mywindow) {
  196.         drawwindow();
  197.         }
  198.         break;
  199.     case activateEvt:    /* activate or deactivate a window */
  200.         if ((WindowPtr) (myevent.message) == mywindow) {
  201.         if (myevent.modifiers & 1) {    /* activate    */
  202.             SetPort(mywindow);
  203.             actmenus();
  204.         } else {            /* deactivate    */
  205.             SetPort(screenport);
  206.             deactmenus();
  207.         }
  208.         }
  209.     }
  210.     }
  211.     return(!userdone);
  212. }
  213.